//
//  WCButton.m
//  WC
//
//  Created by 杨鑫 on 15/4/24.
//  Copyright (c) 2015年 智康. All rights reserved.
//

#import "WCButton.h"
//Macros
#import "Macros.h"

@implementation WCButton

+ (id)txtButton:(NSString *)title frame:(CGRect)frame
{
    WCButton *btn = [[self alloc] initWithFrame:frame];
    
    [btn.layer setCornerRadius:2.0];
    
    [btn setTitle:title forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [btn setTitleFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
    
    btn.titleLabel.textAlignment = NSTextAlignmentCenter;
    btn.titleLabel.font = [UIFont systemFontOfSize:15];
    
    return btn;
}

+ (id)backButton
{
    WCButton *backButton = [[self alloc] initWithFrame:CGRectMake(0, 0, 40, 30)];
    backButton.imageFrame = CGRectMake(0, 0, 40, 30);
    
    [backButton setImage:[UIImage imageNamed:@"PageBack"] forState:UIControlStateNormal];
    backButton.imageView.contentMode = UIViewContentModeCenter;
    
    return backButton;
}

+ (id)closeButton
{
    WCButton *closeButton = [[self alloc] initWithFrame:CGRectMake(0, 0, 40, 30)];
    closeButton.imageFrame = CGRectMake(0, 0, 40, 30);
    
    [closeButton setImage:[UIImage imageNamed:@"PageClose"] forState:UIControlStateNormal];
    closeButton.imageView.contentMode = UIViewContentModeCenter;
    
    return closeButton;
}

+ (id)wcButtonWithType:(WCButtonType)buttonType
{
    WCButton *btn = [[self alloc] initWithFrame:CGRectMake(0, 0, 25, 25)];
    btn.imageFrame = CGRectMake(0, 0, 25, 25);
    btn.imageView.contentMode = UIViewContentModeCenter;
    switch (buttonType) {
        case WCButtonTypeAdd:{
            [btn setImage:[UIImage imageNamed:@"Button_add"] forState:UIControlStateNormal];
            break;
        }
        case WCButtonTypeList:{
            [btn setImage:[UIImage imageNamed:@"Button_list"] forState:UIControlStateNormal];
            break;
        }
        case WCButtonTypeSave:{
            [btn setImage:[UIImage imageNamed:@"Button_save"] forState:UIControlStateNormal];
            break;
        }
        case WCButtonTypeComment:{
            [btn setImage:[UIImage imageNamed:@"Button_comment"] forState:UIControlStateNormal];
            break;
        }
        case WCButtonTypePatient:{
            [btn setImage:[UIImage imageNamed:@"Button_patient"] forState:UIControlStateNormal];
            break;
        }
    }
    
    return btn;
}

- (void)setTitleColor:(UIColor *)color
{
    [self setTitleColor:color forState:UIControlStateNormal];
}

#pragma mark - Setter

- (void)setBg:(UIColor *)bg
{
    _bg = bg;
    self.backgroundColor = bg;
}

#pragma mark - OverWrite

- (void)setHighlighted:(BOOL)highlighted
{
    [super setHighlighted:highlighted];
    if (self.bg && self.downBg) {
        if (highlighted) {
            [self setBackgroundColor:self.downBg];
        } else {
            [self setBackgroundColor:self.bg];
        }
    }
}

- (CGRect)imageRectForContentRect:(CGRect)contentRect
{
    if (self.imageFrame.size.width == 0 || self.imageFrame.size.height == 0) {
        return [super imageRectForContentRect:contentRect];
    } else {
        return self.imageFrame;
    }
    
}
- (CGRect)titleRectForContentRect:(CGRect)contentRect
{
    if (self.titleFrame.size.width == 0 || self.titleFrame.size.height == 0) {
        return [super titleRectForContentRect:contentRect];
    } else {
        return self.titleFrame;
    }
}

@end
